package com.totcy.atom.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.totcy.atom.R;
import com.totcy.atom.bean.QuestionBean;
import com.totcy.atom.utils.ImageGetter.DetailTagHandler;
import com.totcy.atom.utils.ImageGetter.MyImageGetter;

import java.util.List;

public class QuestionPagerAdapter extends PagerAdapter {
    private Context mContext;
    private List<QuestionBean> questions;
    private int type = 0;

    public QuestionPagerAdapter(Context mContext, List<QuestionBean> questions, int type) {
        this.mContext = mContext;
        this.questions = questions;
        this.type = type;
    }

    @Override
    public int getCount() {
        return questions.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    @NonNull
    @Override
    public View instantiateItem(@NonNull ViewGroup container, int position) {
        final QuestionBean bean = questions.get(position);

        View view = LayoutInflater.from(mContext).inflate(R.layout.item_question, container, false);
        TextView content = view.findViewById(R.id.tv_contentHtml);
        content.setText(Html.fromHtml(bean.getContent_html(), new MyImageGetter(mContext, content), new DetailTagHandler(mContext)));
        //设置超链接可以打开网页//click must
        content.setMovementMethod(LinkMovementMethod.getInstance());
        LinearLayout llSelectionRoot = view.findViewById(R.id.ll_selectionRoot);
        if (type == 1) {//选择题
            llSelectionRoot.setVisibility(View.VISIBLE);
            if (bean.getSelections() != null) {
                //创建选项布局
                for (int i = 0; i < bean.getSelections().size(); i++) {
                    View selectionView = LayoutInflater.from(mContext).inflate(R.layout.item_selection, llSelectionRoot, false);
                    selectionView.setBackgroundColor(i % 2 == 0 ? ContextCompat.getColor(mContext, R.color.activity_bg) : ContextCompat.getColor(mContext, R.color.white));
                    final int selectionPosition = i;
                    selectionView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //选过了就不能再选了
                            if (!TextUtils.isEmpty(bean.getSelectAnswer())) {
                                return;
                            }
                            bean.setShowAnswer(true);
                            //设置选中项
                            bean.setSelectAnswer(position2Selection(selectionPosition));
                            notifyDataSetChanged();
                        }
                    });
                    //选项
                    TextView selection = selectionView.findViewById(R.id.tv_selection);
                    selection.setMovementMethod(LinkMovementMethod.getInstance());
                    selection.setText(Html.fromHtml(bean.getSelections().get(i).getContent_html(), new MyImageGetter(mContext, content), new DetailTagHandler(mContext)));
                    //选项A
                    TextView selectionLabel = selectionView.findViewById(R.id.tv_selectionLabel);
                    String strLabel = position2Selection(i);
                    selectionLabel.setText(strLabel);

                    //设置颜色
                    if (TextUtils.isEmpty(bean.getSelectAnswer())) {
                        //未选中设置黑色
                        selection.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_63));
                        selectionLabel.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_63));
                    } else if (bean.getSelectAnswer().equals(bean.getCorrect_answer())) {
                        //正确 && 当前项 == 选择项
                        if (strLabel.equals(bean.getSelectAnswer())) {
                            selection.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                            selectionLabel.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                        } else {
                            //其他项
                            selection.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_63));
                            selectionLabel.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_63));
                        }

                    } else {
                        //错误 && 当前项 == 选择项
                        if (strLabel.equals(bean.getSelectAnswer())) {
                            selection.setTextColor(ContextCompat.getColor(mContext, R.color.colorAccent));
                            selectionLabel.setTextColor(ContextCompat.getColor(mContext, R.color.colorAccent));
                        } else {
                            //其他项
                            selection.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_63));
                            selectionLabel.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_63));
                        }
                        //选出正确的
                        if (strLabel.equals(bean.getCorrect_answer())) {
                            selection.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                            selectionLabel.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                        }
                    }

                    llSelectionRoot.addView(selectionView);
                }
            }

        } else {
            llSelectionRoot.setVisibility(View.GONE);
        }

        //显示、隐藏答案解析
        LinearLayout llRootAnswer = view.findViewById(R.id.ll_answer);
        llRootAnswer.setVisibility(bean.isShowAnswer() ? View.VISIBLE : View.GONE);

        //答案
        TextView tvAnswer = view.findViewById(R.id.tv_correct_answer);
        if (!TextUtils.isEmpty(bean.getCorrect_answer())) {
            tvAnswer.setText(String.format("参考答案：%s", bean.getCorrect_answer()));
        } else {
            tvAnswer.setText("参考答案：");
        }

        TextView tvExplain = view.findViewById(R.id.tv_explain_html);
        if (!TextUtils.isEmpty(bean.getExplain_html())) {
            tvExplain.setMovementMethod(LinkMovementMethod.getInstance());
            tvExplain.setText(Html.fromHtml(bean.getExplain_html(), new MyImageGetter(mContext, content), new DetailTagHandler(mContext)));
        } else {
            tvExplain.setText("");
        }


        container.addView(view);
        return view;
    }

    private String position2Selection(int position) {
        String selectAnswer;
        if (position == 0) {
            selectAnswer = "A";
        } else if (position == 1) {
            selectAnswer = "B";
        } else if (position == 2) {
            selectAnswer = "C";
        } else if (position == 3) {
            selectAnswer = "D";
        } else {
            selectAnswer = "";
        }
        return selectAnswer;
    }
}
